﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using log4net;
using NHibernate;
using NHibernate.Cfg;

namespace SpeakOut.Data.NHibernate
{
    public static class NHibernateHelper
    {
        private static readonly ILog _log = LogManager.GetLogger(typeof(NHibernateHelper));

        public static readonly ISessionFactory SessionFactory;

        static NHibernateHelper()
        {
            _log.Debug("Loading NHibernate configuration");
            try
            {
                Configuration configuration = new Configuration();
                configuration.AddAssembly("SpeakOut.Data");
                SessionFactory = configuration.Configure().BuildSessionFactory();
                _log.Debug("NHibernate configuration loaded");
            }
            catch (Exception ex)
            {
                const string errorMessage = "NHibernate initialization failed";
                _log.Fatal(errorMessage, ex);
                throw new Exception(errorMessage, ex);
            }
        }

        public static ISession OpenSession()
        {
            return SessionFactory.OpenSession();
        }

        public static ISession GetCurrentSession()
        {
            return SessionFactory.GetCurrentSession();
        }
    }
}